package jgl.vajra.kingkong.part.mine.fragment;

import java.util.Map;

import io.reactivex.Observable;
import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.CouponAvailableBean;
import jgl.vajra.kingkong.dialogFragment.UseRulesDialogFragment;
import jgl.vajra.kingkong.manager.AccountManager;
import jgl.vajra.kingkong.part.mine.contract.CouponPageListContract;
import jgl.vajra.kingkong.part.mine.viewModel.CouponPageListViewModel;
import jgl.vajra.kingkong.widget.base.BaseListFragment;
import jgl.vajra.kingkong.widget.bean.BaseListData;
import jgl.vajra.kingkong.widget.interfaces.RefreshRecyclerLoadStatusListener;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.recyclerView.RefreshRecyclerNetConfig;
import jgl.vajra.kingkong.widget.utils.TimeUtils;
import jgl.vajra.kingkong.widget.utils.UIUtils;

@CreateViewModel(CouponPageListViewModel.class)
public class CouponPageListFragment extends BaseListFragment<CouponPageListViewModel, String> implements CouponPageListContract.View {

    private int type;
    private String authorization;
    private UseRulesDialogFragment useRulesDialogFragment;

    @Override
    protected RefreshRecyclerNetConfig getRefreshRecyclerNetConfig() {
        return new RefreshRecyclerNetConfig() {
            @Override
            public Observable getNetObservable(Map<String, Object> map, int action) {
                map.put("Authorization", authorization);
                map.put("tag", type);
                return mViewModel.getCouponList(map);
            }
        };
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_coupon_page;
    }

    @Override
    public void onItemClick(int position, String itemData) {

    }

    @Override
    public void initView() {
        super.initView();
        type = getArguments().getInt("type");

        authorization = AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization;

        mBinding.recyclerView.setIsDataObject(true);
        mBinding.recyclerView.setRefreshLoaderMore(false, false);
        mBinding.recyclerView.setRefreshRecyclerLoadStatusListener(new RefreshRecyclerLoadStatusListener<BaseListData<CouponAvailableBean>>() {
            @Override
            public void onSucceed(BaseListData<CouponAvailableBean> data, int action) {
                mBinding.recyclerView.setRefreshLoaderMore(true, true);

                for (int i = 0; i < data.data.size(); i++) {
                    data.data.get(i).couponType=type;
                    if (type==1){
                        long expirationTime = TimeUtils.parsDataTimeYMDLong(data.data.get(i).endDate);
                        long currentTime = System.currentTimeMillis();
                        long  newExpirationTime= expirationTime-(86400000L *data.data.get(i).vergeOfDate);
                        if(newExpirationTime<currentTime){
                            data.data.get(i).isVergeOfDate=true;
                        }
                    }else if (type==2){
                        data.data.get(i).couponTypeText="已使用";
                    }else if (type==3){
                        data.data.get(i).couponTypeText="已过期";
                    }
                }

                mBinding.recyclerView.requestNetObjectSuccess(data.data, action);
            }

            @Override
            public void onError(String errorInfo, int errorCode, int action) {
                super.onError(errorInfo, errorCode, action);
//                mBinding.recyclerView.requestNetError(errorInfo, errorCode);
            }
        });
        mBinding.recyclerView.firstLoad();
        mBinding.recyclerView.setRecyclerViewBackgroundRes(R.color.gray_f7);
    }

    @Override
    public void onDetails(CouponAvailableBean itemData) {

    }

    @Override
    public void onUseRules(CouponAvailableBean itemData) {
        if (useRulesDialogFragment == null)
            useRulesDialogFragment = new UseRulesDialogFragment();
        useRulesDialogFragment.setData(itemData);
        useRulesDialogFragment.show(mActivity.getSupportFragmentManager());
    }
}
